GROUP BY p.product_id ORDER BY CASE WHEN p.quantity > 0 THEN 0 ELSE 1 END, p.sort_order desc, p.date_added desc